//
//  AppointDoctorView.m
//  仿真开发
//
//  Created by Cloud on 2017/3/24.
//  Copyright © 2017年 B.HOPE. All rights reserved.
//

#import "AppointDoctorView.h"
#import "HMReputationDoctoModel.h"
#import "HMDocDeatil.h"
#import "HMDocDeatilView.h"
#import "HMApplyTimeCollection.h"
#import <Masonry.h>
#import "HMbounceTool.h"


@interface AppointDoctorView ()<UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *iconImage;

@property (weak, nonatomic) IBOutlet UILabel *leftLabel;
@property (weak, nonatomic) IBOutlet UILabel *middleLabel;
@property (weak, nonatomic) IBOutlet UILabel *rightLabel;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

//医生简介模型数组
@property(strong,nonatomic)NSArray<HMDocIntroduceModel *> *intros;

// 医生接诊条件模型数组
@property(strong,nonatomic)NSArray<HMDocRequireModel *> *requires;


@property(strong,nonatomic)NSMutableArray<UIView *> *viewArrs;

@property(weak,nonatomic)UILabel *require;
@property(weak,nonatomic)UILabel *introduction;
@property(assign,nonatomic)BOOL isSelect;

@property(strong,nonatomic)UIView *indicator;


@property(strong,nonatomic)HMApplyTimeCollection *applyCollection;

@end


@implementation AppointDoctorView

- (void)viewDidLoad {
    [super viewDidLoad];
    [self loadData];
    
    _intros=[NSArray array];
    _requires=[NSArray array];
    self.viewArrs = [NSMutableArray arrayWithCapacity:3];
    
    [self setupUI];
    
    
    //查看医生接诊条件
    //NSString *url = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
    //NSDictionary *parameters = @{@"doctor_id":@300000315};
    //获取医生接诊条件数据
    
    
    NSString *introURL = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
    NSDictionary *introsParameter = @{@"doctor_id":@300000315};
    
    [HMDocIntroduceModel getIntrodDataWithURLString:introURL andParameters:introsParameter andSuccessBlock:^(NSArray *newsList) {
        
        self.requires = newsList;
        HMDocIntroduceModel *model =  newsList[0];
        self.introduction.text = model.introduction;
        
    } andErrorBlock:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
    
    
    
    // 医生简介接口
    //NSString *url = @"http://iosapi.itcast.cn/doctor/getIntroduction.json.php";
    //NSDictionary *parameters = @{@"doctor_id":@300000315};
    // 获取医生简介数据
    NSString *conditionURL = @"http://iosapi.itcast.cn/doctor/doctorReceivingSetting.json.php";
    
    NSDictionary *conditionParameter = @{@"doctor_id":@300000315};
    [HMDocRequireModel getDocRequireDataWithURLString:conditionURL andParameters:conditionParameter  andSuccessBlock:^(NSArray *newsList) {
        
        self.intros = newsList;
        
        HMDocRequireModel *model =  newsList[0];
        self.require.text = model.receiving_setting_extra;
        
    } andErrorBlock:^(NSError *error) {
        NSLog(@"error = %@",error);
    }];
    
    
}


-(void)setupUI{
    
    //导航栏右上角的收藏键
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"saveTemplateEmpty"] style:UIBarButtonItemStylePlain target:self action:@selector(clickNavRightBarBtn:)];
    
    //头像设置
    self.iconImage.layer.masksToBounds = YES;
    self.iconImage.layer.cornerRadius = self.iconImage.bounds.size.width*0.5;
    self.iconImage.layer.borderWidth = 1;
    self.iconImage.layer.borderColor = [UIColor whiteColor].CGColor;
    
    
    
    //设置中间滚动条
    UIView *v = [[UIView alloc]init];
    v.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:v];
    [v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view);
        make.top.equalTo(self.leftLabel.mas_bottom).mas_offset(0.5);
        make.height.mas_equalTo(2);
        make.width.equalTo(self.leftLabel);
    }];
    v.layer.cornerRadius = 0.2;
    self.indicator = v;
    
    
    //设置label及其tag值
    //self.leftLabel.textColor = [UIColor yellowColor];
    self.leftLabel.tag = 1;
    self.leftLabel.userInteractionEnabled = YES;
    
    //self.middleLabel.textColor =[UIColor yellowColor];
    self.middleLabel.tag = 2;
    self.middleLabel.userInteractionEnabled = YES;
    
    //self.rightLabel.textColor =[UIColor yellowColor];
    self.rightLabel.tag = 3;
    self.rightLabel.userInteractionEnabled = YES;
    

    //添加手势
    UITapGestureRecognizer *tapLeft = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.leftLabel addGestureRecognizer:tapLeft];
    
    UITapGestureRecognizer *tapMid = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.middleLabel addGestureRecognizer:tapMid];
    
    UITapGestureRecognizer *tapRight = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickLabel:)];
    [self.rightLabel addGestureRecognizer:tapRight];
    
    
    
    // 设置scrollview相关属性
    _scrollView.showsHorizontalScrollIndicator=YES;
    _scrollView.pagingEnabled=YES;
    
    [self.scrollView setBackgroundColor:[UIColor whiteColor]];
    
    [self setBottomUI];
    
    self.scrollView.delegate = self;
    
}

//scrollView上的视图
-(void)setBottomUI{
    
    //定义一个宽
    CGFloat width = self.view.bounds.size.width;
    //定义一个高
    CGFloat height = self.scrollView.bounds.size.height+3;
    
    //定义个数
    int index = 3;
    
    for (int i = 0; i < index; i++) {
        
        UIView *bView = [[UIView alloc]init];
        
        //添加到ScrollView上
        [self.scrollView addSubview:bView];
        
        [self.viewArrs addObject:bView];
        
        bView.frame = CGRectMake(width*i, 0, width, height);
    }
    
    //设置偏移量
    self.scrollView.contentSize = CGSizeMake(width*index, 0);
    
    //设置接诊条件和医生简介(label)
   [self setLabel];
   [self setApplyView];
    [self setQuestion];
    
}



-(void)setLabel{
    
    //接诊条件文字
    UILabel *requirText = [[UILabel alloc]init];
    
    requirText.text=@"有钱吗?";
    [self.viewArrs[0] addSubview:requirText];
    requirText.textAlignment = NSTextAlignmentLeft;
   
 
    [requirText mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.viewArrs[0]).mas_offset(15);
        make.right.equalTo(self.viewArrs[0]).mas_offset(-15);
    }];
  
    requirText.numberOfLines = 0;
    
    self.require = requirText;
    
    
    //医生简介
    UILabel *intro = [[UILabel alloc]init];
    intro.text=@"包你满意";
    [self.viewArrs[1] addSubview:intro];
    
    intro.textAlignment = NSTextAlignmentLeft;
    [intro mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self.viewArrs[1]).mas_offset(15);
        make.right.equalTo(self.viewArrs[1]).mas_offset(-15);
    }];
 
    intro.numberOfLines = 0;
    self.introduction = intro;
    
}


//MARK:设置预约时间
-(void)setApplyView{
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    HMApplyTimeCollection *col = [[HMApplyTimeCollection alloc]initWithCollectionViewLayout:flowLayout];
    
    [self.viewArrs[2] addSubview:col.collectionView];
    
    flowLayout.itemSize = CGSizeMake(45, 48.5);
    
    //if ([UIScreen mainScreen].bounds.size.width > 375) {
    //    flowLayout.itemSize = CGSizeMake(50.6025, 50);
    //}
    
    flowLayout.minimumLineSpacing = 1;
    flowLayout.minimumInteritemSpacing = 0.05;
    flowLayout.sectionInset = UIEdgeInsetsMake(1, 1, 1, 1);
    
    //设置约束
    [col.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.viewArrs[2]);
        make.top.equalTo(self.viewArrs[2]).mas_offset(3);
        make.height.mas_equalTo(200);
    }];
    
    //记录(否则点击会销毁)
    _applyCollection=col;
}




//MARK:点击label触发
-(void)clickLabel:(UITapGestureRecognizer *)sender{
    
    if (sender.view.tag == 1) {
        
        //原地不动
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        
        [UIView animateWithDuration:0.8 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view);
                make.top.equalTo(self.leftLabel.mas_bottom).mas_offset(0.5);
            }];
        }];
    }
    else if(sender.view.tag == 2){
       
        //滚一个宽度
        [self.scrollView setContentOffset:CGPointMake(self.view.bounds.size.width, 0) animated:YES];
        
        [UIView animateWithDuration:0.8 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/3);
                make.top.equalTo(self.middleLabel.mas_bottom).mas_offset(0.5);
            }];
        }];
    }
    else if(sender.view.tag == 3){
        
        //滚2个宽度
        [self.scrollView setContentOffset:CGPointMake(self.view.bounds.size.width*2, 0) animated:YES];
        
        [UIView animateWithDuration:0.8 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/3*2);
                make.top.equalTo(self.rightLabel.mas_bottom).mas_offset(0.5);
            }];
        }];
    }
}

#pragma mark - scrollview代理方法

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    CGFloat width = self.view.bounds.size.width;
    NSInteger index = self.scrollView.contentOffset.x/width;
    
    if (index == 0) {
        [UIView animateWithDuration:1 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view);
                make.top.equalTo(self.leftLabel.mas_bottom);
            }];
        }];
        
    }else if (index == 1){
        [UIView animateWithDuration:1 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/3);
                make.top.equalTo(self.rightLabel.mas_bottom);
            }];
        }];
    }else if(index == 2){
        [UIView animateWithDuration:1.5 animations:^{
            [self.indicator mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.view).mas_offset(self.view.bounds.size.width/3*2);
                //make.top.equalTo(self.applyLabel.mas_bottom);
            }];
        }];
    }
}



//加载数据(顶部视图的数据)
- (void) loadData {
    _topView.repModel = _reModel;
    
}
//接诊条件
-(void)setDocRequireMode:(HMDocRequireModel *)docRequireMode{
    
    NSMutableString *textStr = [NSMutableString string];
    
    for (NSString *s in docRequireMode.receiving_settings) {
        [textStr appendFormat:@"\n\t%@\n",s];
    }
    self.require.text = [NSString stringWithFormat:@"\t%@%@",docRequireMode.receiving_setting_extra,textStr];
}
//医生简介
-(void)setIntroduceModel:(HMDocIntroduceModel *)introduceModel{
    self.introduction.text=introduceModel.introduction;
    
}
//MARK:收藏键的点击事件
-(void)clickNavRightBarBtn:(UIBarButtonItem *)sender{
    if (!self.isSelect) {
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplate"]];
        
        [HMbounceTool CKLShowSVSuccessTitle:@"成功关注" andTime:1];
    }else{
        [self.navigationItem.rightBarButtonItem setImage:[UIImage imageNamed:@"saveTemplateEmpty"]];
        
        [HMbounceTool CKLShowSVSuccessTitle:@"取消关注" andTime:1];
    }
    self.isSelect = ! self.isSelect;
}


//MARK:在线咨询按钮

-(void)setQuestion{
    UIButton *btn = [[UIButton alloc]init];
    btn.backgroundColor = [UIColor lightGrayColor];
    
    [btn setTitle:@"在线客服" forState:UIControlStateNormal];
    [btn setTintColor:[UIColor whiteColor]];
    [self.viewArrs[2] addSubview:btn];
   
    //设置约束
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.viewArrs[2]);
        make.bottom.equalTo(self.view).mas_offset(-16);
        make.width.mas_equalTo(300);
        make.height.mas_equalTo(40);
    }];
    btn.layer.cornerRadius = 10;
    [btn addTarget:self action:@selector(clickForQuestion) forControlEvents:UIControlEventTouchUpInside];
    //self.consult = btn;
}

-(void)clickForQuestion{
    [HMbounceTool CKLShowSVTitle:@"客服去吃饭了" andTime:1.5];
}


@end
